package com.yaoge.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/sharedata")
public class TestShareData {

    @RequestMapping("/testServletAPI")
    public String testServlet(HttpServletRequest request){
        request.setAttribute("yaoge","牛逼");
        return "showdata";
    }
    //测试使用  modelAndView 存放共享数据
    @RequestMapping("/testModelView")
    public ModelAndView testModelView(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("yaoge","耀哥是真的牛逼");
        modelAndView.setViewName("showdata");
        return modelAndView;
    }
    //测试使用  Model  向request中存放数据
    @RequestMapping("/testModel")
    public String testModel(Model model){
      model.addAttribute("yaoge","耀哥是最牛逼的");
        return "showdata";
    }
    //测试使用Map  来存放 request共享数据
    @RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("yaoge","依然很牛逼");
        return "showdata";
    }
    //测试使用ModelMap  来存放 request共享数据
    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("yaoge","白日依山尽，耀哥很牛逼");
        return "showdata";
    }
    //测试  存放 sessiont共享数据
    @RequestMapping("/testSession")
    public String testSession(HttpSession session){
        session.setAttribute("yaoge","session 存储数据  ：耀哥厉害");
        return "showdata";
    }
    //测试  存放 application
    @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("yaoge","application 存储数据  ：耀哥老婆厉害");
        return "showdata";
    }


}
